package com.example.java_message_queue.mqserver.datacenter;

import com.example.java_message_queue.JavaMessageQueueApplication;
import com.example.java_message_queue.mqserver.core.*;
import com.example.java_message_queue.mqserver.mapper.MetaMapper;
import org.apache.ibatis.session.SqlSession;

import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Queue;

public class DataBaseManager {
    // 由于 DataBaseManager 不是一个 Bean
    // 需要手动来获取实例
    private MetaMapper metaMapper;

    public void init() {
        this.metaMapper = JavaMessageQueueApplication.ac.getBean(MetaMapper.class);

        // 构造数据库
        if (!checkDBExists()) {
            // 1. 读取 sql 文件中的内容, 并创建表
            createTable();
            // 2. 插入默认数据
            createDefaultData();
            System.out.println("[DataBaseManager] 数据库初始化完成!");
        } else {
            System.out.println("[DataBaseManager] 数据库已经存在!");
        }
    }

    // 创建数据表
    private void createTable() {
        metaMapper.createUserTable();
        metaMapper.createExchangeTable();
        metaMapper.createQueueTable();
        metaMapper.createBindingTable();
        System.out.println("[DataBaseManager] 创建表完成!");
    }

    // 创建表中的默认数据
    private void createDefaultData() {
        // 构造默认用户
        User user = new User();
        user.setUsername("guest");
        user.setPassword("guest");
        metaMapper.insertUser(user);

        // 构造默认交换机
        Exchange exchange = new Exchange();
        exchange.setName("");
        exchange.setType(ExchangeType.DIRECT);
        exchange.setDurable(true);
        exchange.setAutoDelete(false);
        metaMapper.insertExchange(exchange);
        System.out.println("[DataBaseManager] 创建初始数据完成!");
    }

    private boolean checkDBExists() {
        File file = new File("./meta.db");
        if (file.exists()) {
            return true;
        }
        return false;
    }

    // 删除所有的表
    public void deleteDB() throws IOException {
        File file = new File("./meta.db");
        if (!file.exists()) {
            return;
        }
        boolean ok = file.delete();
        if (!ok) {
            throw new IOException("删除数据库文件失败! ");
        }
    }

    public void insertExchange(Exchange exchange) {
        metaMapper.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName) {
        metaMapper.deleteExchange(exchangeName);
    }

    public List<Exchange> selectAllExchanges() {
        return metaMapper.selectAllExchanges();
    }

    public void insertQueue(MSGQueue queue) {
        metaMapper.insertQueue(queue);
    }

    public void deleteQueue(String queueName) {
        metaMapper.deleteQueue(queueName);
    }

    public List<MSGQueue> selectAllQueues() {
        return metaMapper.selectAllQueues();
    }

    public void insertBinding(Binding binding) {
        metaMapper.insertBinding(binding);
    }

    public void deleteBinding(Binding binding) {
        metaMapper.deleteBinding(binding);
    }

    public List<Binding> selectAllBindings() {
        return metaMapper.selectAllBindings();
    }
}
